package org.byqh.dqeai.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.byqh.dqeai.common.Json;
import org.byqh.dqeai.dto.SysFileCategoryDTO;
import org.byqh.dqeai.dto.common.IPage;
import org.byqh.dqeai.entity.SysFile;
import org.byqh.dqeai.entity.SysFileCategory;
import org.byqh.dqeai.entity.proxy.SysFileCategoryProxy;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sysFileCategory")
@Tag(name = "系统[SysFileCategory] - 文件分类管理")
public class SysFileCategoryController extends BaseController<SysFileCategory, SysFileCategoryProxy> {
    /**
     * 分页查询数据
     */
    @PostMapping(value = "/list")
    public Json<IPage<SysFileCategory>> list(@RequestParam(defaultValue = "1") Integer page,
                                             @RequestParam(defaultValue = "10") Integer size,
                                             @RequestParam(defaultValue = "", required = false) String sort,
                                             @RequestBody(required = false) SysFileCategory dto) {

        return listPageImpl(SysFileCategory.class, page, size, null);
    }

    /**
     * 通过app_id查询数据
     */
    @PostMapping(value = "/listByAppId")
    public Json<List<SysFileCategory>> listByAppId(@RequestParam String appId) {
        String sql = String.format("""
                select *
                        from SYS_FILE_CATEGORY t
                        where category_id in
                              (select file_category_id from SYS_APP_FILE_CATEGORY where app_id = '%s')
                """, appId);

        return Json.success(this.getEntityQueryableBySql(sql).toList());
    }

    /**
     * 通过module_id查询数据
     */
    @PostMapping(value = "/listByModuleId")
    public Json<List<SysFileCategory>> listByModuleId(@RequestParam String moduleId) {
        String sql = String.format("""
                select *
                      from SYS_FILE_CATEGORY t
                      where category_id in
                            (select file_category_id from SYS_APP_MODULE_FILE_CATEGORY where app_module_id = '%s')
                """, moduleId);

        return Json.success(this.getEntityQueryableBySql(sql).toList());
    }

    /**
     * 通过setup_id查询数据
     */
    @PostMapping(value = "/listBySetupId")
    public Json<List<SysFileCategoryDTO>> listBySetupId(
            @RequestParam String setupId,
            @RequestParam Boolean withFile
    ) {
        String sql = String.format("""
                 select distinct *
                      from SYS_FILE_CATEGORY t
                      where category_id in
                            (select file_category_id
                             from SYS_APP_FILE_CATEGORY
                             where app_id in
                                   (select app_id from sys_setup_app where setup_id = '%s'))
                """, setupId);

        List<SysFileCategoryDTO> result = this.getEntityQueryableBySql(sql)
                .selectAutoInclude(SysFileCategoryDTO.class)
                .toList();

        for (var item : result) {
            if (withFile) {
                item.setFileList(this.easyEntityQuery.queryable(SysFile.class)
                        .where(s -> s.categoryId().eq(item.getCategoryId()))
                        .toList());
            }
        }

        return Json.success(result);
    }
}